import {WeaTools} from 'ecCom';

/**
 * 获取本地缓存数据
 *
 * @param key1
 * @param key2
 * @returns {*}
 */
export const getLSValue = (key1, key2) => {
    const obj = WeaTools.ls.getJSONObj(key1);
    return obj && obj[key2];
};

/**
 * 设置本地缓存数据
 *
 * @param key1
 * @param key2
 * @param value
 * @returns {*}
 */
export const setLSValue = (key1, key2, value) => {
    const obj = WeaTools.ls.getJSONObj(key1) || {};
    obj[key2] = value;
    WeaTools.ls.set(key1, obj);
};

/**
 * 初始化本地缓存数据
 *
 * @param loginid 登录账号
 * @param languageidweaver 登录语言
 */
export const initLSValue = (loginid, languageidweaver) => {
    const _loginid = WeaTools.ls.getStr('loginid');
    const _languageidweaver = WeaTools.ls.getStr('languageidweaver');

    const ls = window.localStorage;
    const keys = [];
    if (loginid != _loginid) {
        for (let i = 0, len = ls.length; i < len; i++) {
            const key = ls.key(i);
            if (/^theme-/.test(key) || /^portal-/.test(key) || /^edata-/.test(key) || /^tabdata-/.test(key)) {
                keys.push(key);
            }
        }
    }
    if (languageidweaver != _languageidweaver) {
        keys.push('theme-backEndMenu');
        keys.push('theme-topMenu');
        keys.push('theme-portalMenu');
    }
    for (let j = 0, len2 = keys.length; j < len2; j++) {
        ls.removeItem(keys[j]);
    }

    WeaTools.ls.set('loginid', loginid);
    WeaTools.ls.set('languageidweaver', languageidweaver);
};

/**
 * 获取登录成功后跳转的默认路径名
 *
 * @returns {string}
 */
export const getDefaultPathname = () => {
    return '/main';
};


/**
 * 获取登录背景图片样式
 *
 * @param loginInfo 登录背景图片信息
 * @returns {*}
 */
export const getLoginBgImageStyle = (loginInfo) => {
    const {fillStyle} = loginInfo;
    const src = getLSValue('login', 'loginBgImage') || loginInfo.imgsrc;
    const width = getLSValue('login', 'loginBgImageWidth') || loginInfo.width;
    const height = getLSValue('login', 'loginBgImageHeight') || loginInfo.height;

    let style = {backgroundImage: 'url(' + src + ')'};
    switch (fillStyle) {
        case 'adapt':
        case 'fill':
            style.backgroundRepeat = 'no-repeat';
            style.backgroundSize = 'cover';
            let _style = {};
            let win_width = jQuery(window).width();
            let win_height = jQuery(window).height();
            win_width = win_width < 800 ? 800 : win_width;
            win_height = win_height < 712 ? 712 : win_height;
            let rate = width / height;
            let _rate = win_width / win_height;
            if (rate > _rate) {
                if (fillStyle == 'adapt') {
                    _style = {
                        width: win_width,
                        height: win_width / rate,
                        marginTop: (win_height - win_width / rate) / 2
                    }
                }
                if (fillStyle == 'fill') {
                    _style = {
                        width: win_height * rate,
                        height: win_height,
                        marginLeft: -((win_height * rate - win_width) / 2)
                    }
                }
            } else if (rate < _rate) {
                if (fillStyle == 'adapt') {
                    _style = {
                        width: win_height * rate,
                        height: win_height,
                        marginLeft: (win_width - win_height * rate) / 2
                    }
                }
                if (fillStyle == 'fill') {
                    _style = {
                        width: win_width,
                        height: win_width / rate
                    }
                }
            } else {
                _style = {
                    width: win_width,
                    height: win_height
                }
            }
            style = {...style, ..._style};
            break;
        case 'tile':
            style.backgroundRepeat = 'repeat';
            break;
        case 'center':
            style.backgroundPosition = 'center';
            style.backgroundRepeat = 'no-repeat';
            break;
        default:
            style.backgroundRepeat = 'no-repeat';
            style.backgroundSize = '100% 100%';
            break;
    }
    return style;
};
